<template>
  <div class="p-16px">
    <el-card>
      <el-space wrap :size="16">
        <el-button type="primary" @click="handleCreate">添加</el-button>
      </el-space>
    </el-card>
    <el-card class="mt-16px">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        row-key="id"
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="adName" label="广告名称" min-width="180" />
        <el-table-column label="操作" min-width="180">
          <template #default="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button type="danger" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt-16px flex justify-end">
        <el-pagination
          v-model:current-page="params.current"
          v-model:page-size="params.size"
          layout="prev, pager, next"
          background
          :total="total"
          @current-change="fetchList"
        />
      </div>
    </el-card>
    <el-dialog title="添加广告" v-model="isShowDialog" style="width: 680px">
      <el-form :model="form" label-width="120px">
        <el-form-item label="广告名称">
          <el-input v-model="form.adName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="上传类型">
          <el-radio-group
            v-model="form.uploadType"
            @change="handleUploadTypeChange"
          >
            <el-radio :value="1">图片</el-radio>
            <el-radio :value="2">视频</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="广告图片">
          <el-upload
            :action="commonStore.common.uploadUrl"
            list-type="picture-card"
            :accept="form.uploadType === 1 ? 'image/*' : 'video/*'"
            v-model:file-list="fileList"
            :headers="commonStore.common.headers"
            :data="commonStore.common.data"
            :limit="5"
          >
            <el-icon v-if="fileList.length < 5">
              <Plus />
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">提交</el-button>
          <el-button @click="isShowDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import { useCommonStoreHook } from "@/store/modules/common";

const commonStore = useCommonStoreHook();
const params = ref({
  size: 10,
  current: 1,
});
const fileList = ref([]);
const loading = ref(false);
const isShowDialog = ref(false);
const defaultForm = {
  adName: "",
  adPicUrl: "",
  uploadType: 1,
};
const form = ref({ ...defaultForm });
const tableData = ref([
  {
    id: 1,
    adName: "广告名称",
    adPicUrl: "https://jsonplaceholder.typicode.com/posts/",
  },
]);
const total = ref(0);
const handleSearch = () => {
  params.value.current = 1;
  fetchList();
};
const handleSubmit = () => {
  console.log(form.value);
};
const fetchList = () => {
  loading.value = true;
  console.log(params.value);
  setTimeout(() => {
    loading.value = false;
  }, 1000);
};
const handleCreate = () => {
  form.value = { ...defaultForm };
  isShowDialog.value = true;
};
const handleEdit = (row) => {
  form.value = { ...row };
  isShowDialog.value = true;
};
const handleUploadTypeChange = () => {
  fileList.value = [];
};

const handleDelete = (row) => {
  ElMessageBox.confirm("确定要删除吗？", "提示", {
    type: "warning",
    beforeClose: (action, instance, done) => {
      if (action === "confirm") {
        instance.confirmButtonLoading = true;
        instance.confirmButtonText = "删除中...";
        commonStore
          .delAd({ id: row.id })
          .then(() => {
            done();
            ElMessage.success("删除成功");
            handleSearch();
          })
          .finally(() => {
            instance.confirmButtonLoading = false;
            instance.confirmButtonText = "确定";
          });
      } else {
        done();
      }
    },
  }).then(() => {
    console.log("删除广告", row);
  });
};
const handleUploadSuccess = (res, field) => {
  form.value[field] = res.data.url;
};
onMounted(() => {
  handleSearch();
});
</script>

<style lang="scss" scoped></style>
